'use strict'
const path = require('path')
// gzip压缩插件
const CompressionWebpackPlugin = require('compression-webpack-plugin');//压缩插件
function resolve(dir) {
  return path.join(__dirname, dir)
}
const isProd = process.env.NODE_ENV === 'production';
module.exports = {

  publicPath: "/",
  lintOnSave: false,
  devServer: {
    open: false, //告诉dev-server在服务器启动后打开浏览器 boolean = false string object
    port: 80, //指定端口号
    host: '0.0.0.0', //指定要使用的主机
    https: false, //默认情况下，将通过HTTP为dev-server提供服务
    hotOnly: false //启用热模块替换
  },
  chainWebpack(config) {
    config.plugins.delete('preload') // TODO: need test
    config.plugins.delete('prefetch') // TODO: need test

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
    config.resolve.alias.set('@', resolve('src')).set('Public',resolve('public'))
  },
  configureWebpack: config => {
		let plugins = [
			new CompressionWebpackPlugin({//压缩插件
				filename: '[path].gz[query]',
				algorithm: 'gzip',
				test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
				threshold: 10240,
				minRatio: 0.8
			})
		];
		if (isProd) {//性能优化
			config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true;
			config.performance = {
				hints: 'warning', //入口起点的最大体积
				maxEntrypointSize: 50000000, //生成文件的最大体积
				maxAssetSize: 30000000, //只给出 js 文件的性能提示
				assetFilter: function (assetFilename) {
					return assetFilename.endsWith('.js');
				}
			};
			// config.externals = externals;
			config.plugins = [
				...config.plugins,
				...plugins
			];
		}
	}
  // configureWebpack: {
  //   // plugin:[
  //   //   new SkeletonPlugin({
  //   //   })
  //   // ]
  // }
}
